﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using Repid.Core.Configuration;

namespace Repid.Core.Context
{
    /// <summary>
    /// 数据上下文
    /// </summary>
    internal partial class EntitiesContext : DbContext
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        internal EntitiesContext(string connString) : base(connString)
        {
            // 禁用实体状态改变跟踪
            Configuration.AutoDetectChangesEnabled = false;
            // 禁用导航属性延迟加载
            Configuration.LazyLoadingEnabled = false;
            // 禁用自动创建代理类
            Configuration.ProxyCreationEnabled = false;
            // 禁用数据库null语义
            Configuration.UseDatabaseNullSemantics = true;
            // 禁用验证所跟踪实体
            Configuration.ValidateOnSaveEnabled = false;
        }

        /// <summary>
        /// 模型配置重写
        /// </summary>
        /// <param name="modelBuilder">数据实体生成器</param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // 禁用一对多级联删除
            //modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            // 禁用多对多级联删除
            //modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

            // 初始化实体模型生成器
            EntityConfigurationFactory.ConfigurationsInit(modelBuilder.Configurations);
        }
    }
}